﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Abs.Core;

namespace Abs.App.Admin
{
    public partial class Manage_Rooms : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            //Kicks the non admin accounts out
            if (Session["Role"] == null)
                Response.Redirect("/User/Unauthorized_Access.aspx");
            if ((int)Session["Role"] == (int)Abs.Core.Models.User.RoleEnum.User)
                Response.Redirect("/User/Unauthorized_Access.aspx");

            var roomManager = Abs.Core.Management.Manage.Room;
            IList<Abs.Core.Models.Room.Room> roomList = roomManager.SelectAllRoom();
            IList<Abs.Core.Models.Room.RoomSchedule> scheduleList = roomManager.SelectAllRoomSchedules();
            
            foreach (Abs.Core.Models.Room.Room room in roomList)
            {
                TableRow row = new TableRow();
                string startTime = "";
                string endTime = "";
                for (int i = 0; i < 11; i++)
                {
                    TableCell cell = new TableCell();
                    if (i == 0)
                    {
                        Label label = new Label();
                        label.Text = room.Name;
                        cell.Controls.Add(label);
                    }
                    else if (i == 1)
                    {
                        CheckBox checkbox = new CheckBox();
                        checkbox.Enabled = false;
                        foreach (Abs.Core.Models.Room.RoomSchedule rschedule in scheduleList)
                        {
                            if (rschedule.RoomId == room.RoomId)
                            {
                                if (rschedule.DayId == (int)DayOfWeek.Monday)
                                {
                                    checkbox.Checked = true;
                                    startTime = rschedule.StartTime.ToShortTimeString();
                                    endTime = rschedule.EndTime.ToShortTimeString();
                                }
                            }
                        }
                        cell.Controls.Add(checkbox);
                    }
                    else if (i == 2)
                    {
                        CheckBox checkbox = new CheckBox();
                        checkbox.Enabled = false;
                        foreach (Abs.Core.Models.Room.RoomSchedule rschedule in scheduleList)
                        {
                            if (rschedule.RoomId == room.RoomId)
                            {
                                if (rschedule.DayId == (int)DayOfWeek.Tuesday)
                                {
                                    checkbox.Checked = true;
                                    startTime = rschedule.StartTime.ToShortTimeString();
                                    endTime = rschedule.EndTime.ToShortTimeString();
                                }
                            }
                        }
                        cell.Controls.Add(checkbox);
                    }
                    else if (i == 3)
                    {
                        CheckBox checkbox = new CheckBox();
                        checkbox.Enabled = false;
                        foreach (Abs.Core.Models.Room.RoomSchedule rschedule in scheduleList)
                        {
                            if (rschedule.RoomId == room.RoomId)
                            {
                                if (rschedule.DayId == (int)DayOfWeek.Wednesday)
                                {
                                    checkbox.Checked = true;
                                    startTime = rschedule.StartTime.ToShortTimeString();
                                    endTime = rschedule.EndTime.ToShortTimeString();
                                }
                            }
                        }
                        cell.Controls.Add(checkbox);
                    }
                    else if (i == 4)
                    {
                        CheckBox checkbox = new CheckBox();
                        checkbox.Enabled = false;
                        foreach (Abs.Core.Models.Room.RoomSchedule rschedule in scheduleList)
                        {
                            if (rschedule.RoomId == room.RoomId)
                            {
                                if (rschedule.DayId == (int)DayOfWeek.Thursday)
                                {
                                    checkbox.Checked = true;
                                    startTime = rschedule.StartTime.ToShortTimeString();
                                    endTime = rschedule.EndTime.ToShortTimeString();
                                }
                            }
                        }
                        cell.Controls.Add(checkbox);
                    }
                    else if (i == 5)
                    {
                        CheckBox checkbox = new CheckBox();
                        checkbox.Enabled = false;
                        foreach (Abs.Core.Models.Room.RoomSchedule rschedule in scheduleList)
                        {
                            if (rschedule.RoomId == room.RoomId)
                            {
                                if (rschedule.DayId == (int)DayOfWeek.Friday)
                                {
                                    checkbox.Checked = true;
                                    startTime = rschedule.StartTime.ToShortTimeString();
                                    endTime = rschedule.EndTime.ToShortTimeString();
                                }
                            }
                        }
                        cell.Controls.Add(checkbox);
                    }
                    else if (i == 6)
                    {
                        CheckBox checkbox = new CheckBox();
                        checkbox.Enabled = false;
                        foreach (Abs.Core.Models.Room.RoomSchedule rschedule in scheduleList)
                        {
                            if (rschedule.RoomId == room.RoomId)
                            {
                                if (rschedule.DayId == (int)DayOfWeek.Saturday)
                                {
                                    checkbox.Checked = true;
                                    startTime = rschedule.StartTime.ToShortTimeString();
                                    endTime = rschedule.EndTime.ToShortTimeString();
                                }
                            }
                        }
                        cell.Controls.Add(checkbox);
                    }
                    else if (i == 7)
                    {
                        Label label = new Label();
                        label.Text = startTime;
                        cell.Controls.Add(label);
                    }
                    else if (i == 8)
                    {
                        Label label = new Label();
                        label.Text = endTime;
                        cell.Controls.Add(label);
                    }

                    else if (i == 9)
                    {
                        HyperLink link = new HyperLink();
                        link.Text = "Edit";
                        link.NavigateUrl = "~/Admin/Edit_Room.aspx?Id=" + room.RoomId.ToString();
                        cell.Controls.Add(link);
                    }
                    else if (i == 10)
                    {
                        HyperLink link = new HyperLink();
                        link.Text = "Delete";
                        link.NavigateUrl = "~/Admin/Delete_Room.aspx?Id=" + room.RoomId.ToString();
                        cell.Controls.Add(link);
                    }
                    row.Cells.Add(cell);
                }
                Table_Display.Rows.Add(row);
            }
        }
    }
}